// pages/beMember/beMember.js
const common = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        userInfo: null,
        cardGoods: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        isPaying: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            nowDate: new Date().getTime()
        })
        if (wx.getStorageSync('token')) {
            this.getUserInfo()
            this.getCardGoods()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            cardGoods: [],
            nowPage: 0,
            isLoading: false,
            isAll: false,
            isPaying: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getCardGoods()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        let userId = wx.getStorageSync('userId')
        let storeId = wx.getStorageSync('storeId')
        if (userId) {
            return {
                title: '成为锦鲤会员，享多重特权',
                path: '/pages/beMember/beMember?userShare=' + userId
            }
        }
    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
            }
        }).catch(function (e) {})
    },

    getCardGoods: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let cardGoods = this.data.cardGoods
        let data = {
            is_sale: '1',
            is_interval: '1',
            page: this.data.nowPage,
            limit: 10
        }
        common.post('Goodslist/goods_list', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    cardGoods: cardGoods,
                    isAll: true
                })
            } else {
                cardGoods = cardGoods.concat(res)
                that.setData({
                    cardGoods: cardGoods,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) {})
    },

    goGoodsDetail: function (e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    },

    beMember: function () {
        if (this.data.isPaying) {
            return
        }
        this.setData({
            isPaying: true
        })
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    //发起网络请求
                    let data = {
                        code: res.code,
                        pay_client: 1
                    }
                    common.post('Order/order_user_vip', data).then(function (res) {
                        that.setData({
                            isPaying: false
                        })
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.requestPayment({
                            timeStamp: res.timeStamp,
                            nonceStr: res.nonceStr,
                            package: res.package,
                            signType: res.signType,
                            paySign: res.paySign,
                            success(res) {
                                wx.showModal({
                                    title: '提示',
                                    content: '开通成功！',
                                    showCancel: false,
                                    success(res) {
                                        that.setData({
                                            cardGoods: [],
                                            nowPage: 0,
                                            isLoading: false,
                                            isAll: false,
                                            isPaying: false
                                        })
                                        that.getUserInfo()
                                        that.getCardGoods()
                                    }
                                })
                            },
                            fail(res) {
                                wx.showToast({
                                    title: '支付失败',
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                        })
                    }).catch(function (e) {})
                }
            }
        })
    }
})